#ifndef INCLUDE_SNMPDEF_H
#define INCLUDE_SNMPDEF_H

#include <string.h>
#include <stdio.h>
#include "snmptype.h"
#include "snmpfunc.h"

#define DECODE_NO_ERROR             0
#define DECODE_ASN_PARSE_ERROR      -1
#define DECODE_ALLOCATION_FAILURE   -2
#define DECODE_UNKNOWN_PDU_HANDLERS -3
#define DECODE_UNKNOWN_CONTEXTS     -4


#define SNMP_COMMUNITY_ERR_NO_ERROR 0
#define SNMP_COMMUNITY_ERR_BAD_COMM 1
/* Values for pdu_type (class and form bits are not included) */
#define GET_REQUEST_PDU            0
#define GET_NEXT_REQUEST_PDU   	   1
#define GET_RESPONSE_PDU           2
#define SET_REQUEST_PDU            3
#define TRAP_PDU                   4
#define GET_BULK_REQUEST_PDU       5
#define INFORM_REQUEST_PDU         6
#define TRAP2_PDU                  7
#define REPORT_PDU                 8
#define MAX_V1_PDU                 4
#define MAX_PDU                    8
#define NO_PDU                     0xFF

/* Values for error_status      */
#define NO_ERROR                 0
#define TOO_BIG                  1
#define NO_SUCH_NAME             2
#define BAD_VALUE                3
#define READ_ONLY                4
#define GEN_ERR                  5
#define NO_ACCESS                6
#define WRONG_TYPE               7
#define WRONG_LENGTH             8
#define WRONG_ENCODING           9
#define WRONG_VALUE             10
#define NO_CREATION             11
#define INCONSISTENT_VALUE      12
#define RESOURCE_UNAVAILABLE    13
#define COMMIT_FAILED           14
#define UNDO_FAILED             15
#define AUTHORIZATION_ERROR     16
#define NOT_WRITABLE            17
#define INCONSISTENT_NAME       18
#define LAST_ERROR              19

#define USER_ERR                  100

/* ASN.1 basic types, all in UNIVERSAL scope */
#define A_NO_ID             0x00
#define A_BOOLEAN           0x01
#define A_INTEGER           0x02
#define A_BITSTRING         0x03
#define A_OCTETSTRING       0x04
#define A_NULL              0x05
#define A_OBJECTID          0x06
#define A_SEQUENCE          0x10	/*different from asn1.h*/
#define A_SET               0x11

/* ASN.1 FLAGS (form and class), adjusted for position within an octet */
#define A_IDCF_MASK         0xE0	/* Mask for ID CLASS and FORM bits */
#define A_IDC_MASK          0xC0	/* Mask for ID CLASS bits */
#define A_IDF_MASK          0x20	/* Mask for ID FORM bit */
#define A_FLAG_MASK         0xE0
#define A_UNIVERSAL         0x00
#define A_APPLICATION       0x40
#define A_CONTEXT           0x80
#define A_PRIVATE           0xC0
#define A_PRIMITIVE         0x00
#define A_CONSTRUCTOR       0x20
#define A_DEFAULT_SCOPE     A_CONTEXT

/* Values for vb_type      */
#define VT_NUMBER       A_INTEGER
#define VT_STRING       A_OCTETSTRING
#define VT_BITS         A_OCTETSTRING	/* same as a string */
#define VT_OBJECT       A_OBJECTID
#define VT_EMPTY        A_NULL
#define VT_IPADDRESS    (A_APPLICATION | 0)
#define VT_COUNTER      (A_APPLICATION | 1)
#define VT_GAUGE        (A_APPLICATION | 2)
#define VT_UNSIGNED32   (A_APPLICATION | 2)	/* same as a guage */
#define VT_TIMETICKS    (A_APPLICATION | 3)
#define VT_OPAQUE       (A_APPLICATION | 4)
#define VT_COUNTER64    (A_APPLICATION | 6)
#define VT_UINTEGER32   (A_APPLICATION | 7)

/* values for exception data types */
#define VT_NOSUCHOBJ    (A_CONTEXT | 0)
#define VT_NOSUCHINS    (A_CONTEXT | 1)
#define VT_ENDOFMIB     (A_CONTEXT | 2)

enum
{
    TRAP_coldStart=0,
    TRAP_warmStart=1,
    TRAP_linkDown=2,
    TRAP_linkUp=3,
    TRAP_authenticationFailure=4,
    TRAP_enterprise=6
};

/* Values for OID*/
/*PURPOSE:  Compute the number of octets needed to hold an ASN.1 length field.
    PARAMETERS: _UINT16   The proposed length
   RETURNS:    _UINT16   The number of octets needed*/
#define A_SizeOfLength(S) ((_UINT16)((S) <= 127 ? 1 : ((S) <= 255 ? 2 : 3)))

/*PURPOSE:  Compute the number of octets needed to hold an ASN.1 type field
   PARAMETERS: _UINT16           The proposed type (without CLASS & FORM bits)
   RETURNS:    unsigned int        The number of octets needed*/
#define A_SizeOfType(V) ((_UINT32) ((V) <= 30 ? 1 : ((V) <= 127 ? 2 : ((V) <= 16383 ? 3 : 4))))

/*A_SizeOfOctetString --  Return total size that an octet string would
             occupy when ASN.1 encoded (tag and length fields are NOT included) */
#define A_SizeOfOctetString(L)  ((_UINT16)(L))

/****************************************************************************
Decoding errors
****************************************************************************/
#define AE_PREMATURE_END                1
#define AE_INDEFINITE_LENGTH            2
#define AE_WRONG_VALUE                  3
#define AE_WRONG_TYPE                   4
#define AE_WRONG_LENGTH                 5
#define AE_ALLOC_FAILURE                6


#define SNMP_SUCCESS 	1
#define SNMP_FAILURE 	0


/* The following structure is used to internally hold an object identifier */
/* Each component is limited in range from 0 to 64K-1.                     */

//#define TRUE  1
//#define FALSE 0


#define A_DecodeTypeClass(L) ((_UINT8) (Lcl_Peekc((LCL_FILE *)(L)) &  A_IDCF_MASK))

#define SNMP_VERSION_MIN                0	/* Lower bound for SNMP version */
#define VERSION_RFC1067                 0	/* Classic SNMP */
#define VERSION_RFC1098                 0
#define VERSION_RFC1157                 0
#define SNMP_VERSION_1                  0	/* now being tagged as version 1 */
#define SNMP_VERSION_2                  1	/* SNMPV2 */
#define SNMP_VERSION_USEC            2	/* User Security option */
#define SNMP_VERSION_3                  3	/* SNMPv3 */
#define SNMP_VERSION_MAX       0x7fffffff	/* Upper bound for SNMP version */

#define SNMP_TERMINATOR  0
#define SNMP_SCALAR 	 1
#define SNMP_TABLE	 	 2
#define SNMP_NODE 	 	 3

#define COMMUNITY_RO 0
#define COMMUNITY_RW 1

#define SNMP_STATE_ENABLED		1
#define SNMP_STATE_CONNECTED	2
#define SNMP_TRAP_PORT	162	/**< Local SNMP client port that will	be used for sending requests */
#define SNMP_SERVERPORT	161	/**< SNMP server's port */


#define SNMP_ERR_STATUS(rp) (rp->pdu.ext.errID.error_status)
#define SNMP_ERR_INDEX(rp) (rp->pdu.ext.errID.error_index)
#define SNMP_TRAP_GEN(rp) (rp->pdu.ext.trapID.generic)
#define SNMP_TRAP_SPE(rp) (rp->pdu.ext.trapID.specfic)

#ifndef ntohl
#define ntohl(x)	((((x) & 0x000000ff) << 24) | (((x) & 0x0000ff00) <<  8) | \
			 (((x) & 0x00ff0000) >>  8) | (((x) & 0xff000000) >> 24))

#define htonl(x)	((((x) & 0x000000ff) << 24) |  (((x) & 0x0000ff00) <<  8) | \
			 (((x) & 0x00ff0000) >>  8) | (((x) & 0xff000000) >> 24))

#define ntohs(x)	((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))

#define htons(x)	((((x) & 0x00ff) << 8) |  (((x) & 0xff00) >> 8))
#endif

#define SYSNAME_LEN 32

extern COMMUNITY_T *get_community_from_index(_UINT8 idx);
extern TRAP_SERVER_T *get_trapserver_from_index(_UINT8 idx);

extern COMMUNITY_T gCommunity[MAX_COMMUNITY_NUM];
extern TRAP_SERVER_T gTrapServer[MAX_TRAP_SERVER_NUM];
extern SNMP_PKT_T gSnmpPkt;
extern _UINT32 gSysUptimeInSecond;
extern _UINT8  net_buf[];
extern char gSysName[SYSNAME_LEN];
extern char gSysDescr[SYSNAME_LEN];
extern char gSysContact[SYSNAME_LEN];
extern char gSysLocation[SYSNAME_LEN];
#endif

